﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNetX
{
	public static class Int32Extensions
	{
		#region Public Static Methods

		// adapted from http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java/3758880#3758880
		public static String GetDownloadSize(this Int32 i, bool useSimpleFormat = true)
		{
			int unit = useSimpleFormat ? 1000 : 1024;

			if (i < unit)
				return i + " B";

			int exp = (int)(Math.Log(i) / Math.Log(unit));

			String pre = (useSimpleFormat ? "kMGTPE" : "KMGTPE")[exp - 1] + (useSimpleFormat ? "" : "i");

			return String.Format("{0:0.00} {1}B", i / Math.Pow(unit, exp), pre);
		}

		#endregion
	}
}
